﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class MealDAO
    {
        //lay danh sach chi tiet cac mon an
        public List<MealDetailDTO> GetAllMealDetail(string name, string catalogID)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var results = from meal in data.MEALs
                          join catalog in data.CATALOGs on meal.CatalogID equals catalog.ID
                          where meal.Name.IndexOf(name) >= 0 && catalog.ID.IndexOf(catalogID) >= 0 && meal.IsDelete == false
                          select new MealDetailDTO(meal.ID, meal.Name, meal.Unit, Double.Parse(meal.CurrentPrice.ToString()), catalog.Name, meal.Available == true ? true : false);

            List<MealDetailDTO> retVal = new List<MealDetailDTO>();

            retVal = results.ToList<MealDetailDTO>();
            return retVal;
        }

        //lay danh sach cac mon an duoc goi
        public List<FoodReportDTO> GetCalledMeals(string catalogID)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();
            if (catalogID.Equals(""))
            {
                var result = from bd in data.BILL_DETAILs
                             join b in data.BILLs on bd.BillID equals b.ID
                             join m in data.MEALs on bd.MealID equals m.ID
                             where m.IsDelete == false
                             select new FoodReportDTO()
                             {
                                 Name = m.Name,
                                 Amount = Int32.Parse(bd.Amount.ToString())
                             };

                if (result.Count() > 0)
                    return result.ToList<FoodReportDTO>();
            }
            else
            {
                var result = from bd in data.BILL_DETAILs
                             join b in data.BILLs on bd.BillID equals b.ID
                             join m in data.MEALs on bd.MealID equals m.ID
                             where m.IsDelete == false && m.CatalogID.Equals(catalogID)
                             select new FoodReportDTO()
                             {
                                 Name = m.Name,
                                 Amount = Int32.Parse(bd.Amount.ToString())
                             };

                if (result.Count() > 0)
                    return result.ToList<FoodReportDTO>();
            }

            return null;
        }

        public List<FoodReportDTO> GetCalledMealsByDate(DateTime start, DateTime end, string catalogID)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();
            if (catalogID.Equals(""))
            {
                var result = from bd in data.BILL_DETAILs
                             join b in data.BILLs on bd.BillID equals b.ID
                             join m in data.MEALs on bd.MealID equals m.ID
                             where b.CreatedDate.Value.Date >= start.Date && b.CreatedDate.Value.Date <= end.Date && m.IsDelete == false
                             select new FoodReportDTO()
                             {
                                 Name = m.Name,
                                 Amount = Int32.Parse(bd.Amount.ToString())
                             };

                if (result.Count() > 0)
                    return result.ToList<FoodReportDTO>();
            }
            else
            {
                var result = from bd in data.BILL_DETAILs
                             join b in data.BILLs on bd.BillID equals b.ID
                             join m in data.MEALs on bd.MealID equals m.ID
                             where b.CreatedDate.Value.Date >= start.Date && b.CreatedDate.Value.Date <= end.Date && m.IsDelete == false && m.CatalogID.Equals(catalogID)
                             select new FoodReportDTO()
                             {
                                 Name = m.Name,
                                 Amount = Int32.Parse(bd.Amount.ToString())
                             };

                if (result.Count() > 0)
                    return result.ToList<FoodReportDTO>();
            }

            return null;
        }

        //them mon an
        public bool AddMeal(MealDTO mealDTO)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var lastID = from meal in data.MEALs
                         orderby meal.ID descending
                         select meal.ID;

            var mealCode = (from code in data.CODEs
                            where code.Data.Equals("meal")
                            select code).First();

            string mealID = mealCode.Prefix;

            if (lastID.Count() > 0)
            {
                string tmp = lastID.First();
                mealID = Ultilities.GetNextId(tmp, "meal");
            }
            else
            {
                for (int i = 0; i < Int32.Parse(mealCode.PostfixLength.ToString()); i++)
                {
                    mealID += "0";
                }
            }

            mealDTO.ID = mealID;

            try
            {
                MEAL meal = new MEAL();
                meal.ID = mealDTO.ID;
                meal.Name = mealDTO.Name;
                meal.Unit = mealDTO.Unit;
                meal.CurrentPrice = mealDTO.CurrentPrice;
                meal.CatalogID = mealDTO.CatalogID;
                meal.Available = mealDTO.Available;
                meal.IsDelete = false;

                data.MEALs.InsertOnSubmit(meal);
                data.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        //cap nhat thong tin mon an
        public bool EditMeal(MealDTO mealDTO)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from meal in data.MEALs
                          where meal.ID.Equals(mealDTO.ID)
                          select meal;

            if (result.Count() > 0)
            {
                result.First().ID = mealDTO.ID;
                result.First().Name = mealDTO.Name;
                result.First().Unit = mealDTO.Unit;
                result.First().CurrentPrice = mealDTO.CurrentPrice;
                result.First().CatalogID = mealDTO.CatalogID;
                result.First().Available = mealDTO.Available;

                try
                {
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }

        //xoa mon an
        public bool RemoveMeal(string id)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from meal in data.MEALs
                          where meal.ID.Equals(id)
                          select meal;
            if (result.Count() > 0)
            {
                try
                {
                    //data.MEALs.DeleteOnSubmit(result);
                    result.First().IsDelete = true;
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }

        //lay danh sach cac don vi tinh cua mon an
        public List<string> GetAllUnit()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = (from meal in data.MEALs
                          select meal.Unit);
            List<string> retVal = new List<string>();
            if (result.Count() > 0)
            {
                result.Distinct(StringComparer.OrdinalIgnoreCase);
                retVal = result.ToList<string>();
            }
            return retVal;
        }

        //lay gia hien hanh cua mon an
        public double GetPrice(string mealID)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var meals = from m in data.MEALs
                        where m.ID.Equals(mealID)
                        select m;

            if (meals.Count() > 0)
            {
                return double.Parse(meals.First().CurrentPrice.ToString());
            }

            return 0.0F;
        }
    }
}